﻿using System;
using System.Collections.Generic;
using Gp.Input;
using Gp.Scripts.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.GUI {
    public interface IMagicMergeSkill {
        MagicMergeInputEntry[] GetInputEntries();

        void OnInputEnd(int[] inputs);

        void OnUpdateInput(int[] inputs);

        void OnCancel();

        int MaxInput() {
            return 4;
        }
    }


    public class MagicMergeInputEntry {
        public string EntryName;
        public Color EntryColor;
        public Sprite EntryImage;
    }



    public class MagicMergeUIZone : MonoBehaviour {
        [SerializeField] private Transform window;
        [SerializeField] private Transform inputPart;
        private List<Image> _inputsImages;

        private IMagicMergeSkill _curSkill;
        private MagicMergeInputEntry[] _entries;
        private List<int> _curInputs;

        [SerializeField] private TMP_Text[] text;

        private void Start() {
            _inputsImages = new List<Image>();
            _curInputs = new List<int>();
            
            foreach (Transform child in inputPart) {
                if (child.TryGetComponent<Image>(out var img)) {
                    _inputsImages.Add(img);
                }
            }
            Hide();
        }
        

        public void Show(IMagicMergeSkill skill) {
            window.gameObject.SetActive(true);

            _curSkill = skill;
            _entries = skill.GetInputEntries();
            _curInputs.Clear();
            
            for (var i = 0; i < _entries.Length; i++) {
                if (text[i] != null) {
                    text[i].text = _entries[i].EntryName;
                }
            }
        }


        private void Update() {
            if (_curSkill == null) return;
            
            if (Global.Get<InputManager>().CancelButtonDown()) {
                OnCancelInput();
            }
            
            if (!Global.Get<InputManager>().IsSelectionInputKeyDown(out var key)) {
                return;
            }


            Map(key);
        }

        
        private void Map(SelectionInputKey key) {
            int index = (int) key;
            if (index < 0 || index >= _entries.Length) {
                return;
            }
            
            HandleInput(index);
        }



        private void HandleInput(int index) {
            // Global.Get<BattleSystem>().BlockCancel = true;
            
            if (_curSkill.MaxInput() == _curInputs.Count) {
                return;
            }

            var cnt = _curInputs.Count;
            
            if (cnt >= 0 && cnt < _inputsImages.Count) {
                _inputsImages[cnt].gameObject.SetActive(true);
                _inputsImages[cnt].sprite = _entries[index].EntryImage;
                _inputsImages[cnt].color = _entries[index].EntryColor;
            }
            
            _curInputs.Add(index);
            if (_curSkill.MaxInput() == _curInputs.Count) _curSkill.OnInputEnd(_curInputs.ToArray());
        }

        
        private void OnCancelInput() {
            switch (_curInputs.Count) {
                case 1:
                    // Global.Get<BattleSystem>().BlockCancel = false;
                    // Hide();
                    break;
                case 0:
                    return;
            }

            // 移除最后一个元素
            _inputsImages[_curInputs.Count - 1].gameObject.SetActive(false);
            _curInputs.RemoveAt(_curInputs.Count - 1);
            // _curSkill.OnUpdateInput(_curInputs.ToArray());
        }
        
        
        
        

        public void Hide() {
            _curInputs.Clear();
            // Global.Get<BattleSystem>().BlockCancel = false;
            window.gameObject.SetActive(false);
            
            foreach (var img in _inputsImages) {
                img.gameObject.SetActive(false);
            }
            
            _entries = null;
            _curInputs.Clear();
        }
        
    }
}